﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jumpton.SFA.Server.BusinessRule.MasterData;
using Jumpton.SFA.Server.Entity;
using Jumpton.SFA.Server.BusinessRule;
using Jumpton.SFA.Server.BusinessRule.System;
using Jumpton.SFA.Server.BusinessRule.InStoreDataMeasures;
using Jumpton.Persistent;
using Jumpton.SFA.Entity;
public partial class Message_Detail : PageBase
{
    #region 定义变量
  
    private MessageManager messManager = null;  //消息作类
    private Message message = null;              //消息对象
    private int messageID = -1;                  //消息ID号
   
    #endregion

    /// <summary>
    /// Handles the Load event of the Page control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void Page_Load(object sender, EventArgs e)
    {
        int.TryParse(Request.QueryString["Mid"], out messageID);       
        if (!IsPostBack)
        {
            UpdateMessageReceiver();

             //获取消息ID号
             //TODO:异常处理
             if (messageID > 0)
             {
                 messManager = new MessageManager();
                 message = messManager.LoadMessage(messageID);
                 DataTable dt = messManager.GetMessageReceiveList(messageID.ToString());
                 Fill(message);
                 GridPager1.DataSource = dt;
                 GridPager1.GridView = gvMessage;
                 GridPager1.BindData();
                 GridPager1.Visible = gvMessage.Rows.Count > 0;
                

                 DictionaryItemsManager dim = new DictionaryItemsManager();
                 
                 //区域层次
                 ddlMessageLevel.DataTextField = "Name";
                 ddlMessageLevel.DataValueField = "DictionaryItemId";
                 ddlMessageLevel.DataSource = dim.GetItemList(DictionaryEnum.MessageType, this.MyStateManager.DomainID);
                 ddlMessageLevel.DataBind();
                // ddlMessageLevel.Items.Insert(0, new ListItem("--所有--", ""));
                 ddlMessageLevel.SelectedValue = message.CONTENTLEVEL.ToString();

             }

            

        }

       
      
    }



    //TODO:图片上传到统一文件夹中
    protected void Fill(Message mes)
    {
        txtTopic.Text = mes.TOPIC;
        txtStartTime.Text = Jumpton.SFA.Server.BusinessRule.BusinessLogic.GetDateTime(mes.BEGINDATE.ToString(), "yyyyMMdd", "yyyy-MM-dd");
             
        txtEndTime.Text = Jumpton.SFA.Server.BusinessRule.BusinessLogic.GetDateTime(mes.ENDDATE.ToString(), "yyyyMMdd", "yyyy-MM-dd");
          
        //chxActive.Checked = mes.ACTIVE;
        txtContent.Text = mes.CONTENT;
        ddlMessageLevel.SelectedValue = mes.CONTENTLEVEL.ToString();
        PHOTO p=null;
        try
        {
            p = new PhotoManager().Load(mes.PHOTOID);
        }
        catch { }
        if (p != null)
        {
            Image1.ImageUrl = "../UploadImages/" + p.PHOTOPATH;
            Image1.AlternateText = mes.TOPIC;
        }
        else
        {
            Image1.Visible = false;
        }
        
    }
    //TODO:
    private void UpdateMessageReceiver()
    {
        messManager = new MessageManager();
         Message_Receive mr= messManager.LoadMessageReceive(messageID, MyStateManager.PersonID);
        if (mr!=null&&!mr.READFLAG)//更新状态为已读
         {
             mr.READFLAG = true;
             
             messManager.UpdateReadCount(messageID);//更新已读消息数量
         }
    }

   
}
